package com.youngball.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.youngball.business.entity.SysTest;
import com.youngball.business.service.SysTesService;
import com.youngball.business.service.qiniu.SysQiniuService;
import com.youngball.business.utill.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by liaopingzhu on 2017/1/16.
 */
@Controller
@RequestMapping(value = "/systest")
public class SysTestController extends BaseController{

    @Autowired
    private SysTesService sysTesService;

    @Autowired
    private SysQiniuService sysQiniuService;

    @RequestMapping(value = "findAll",method = RequestMethod.GET,headers = "Accept=application/json")
    public ResponseEntity<Map> findAll(@RequestParam(required = true, value = "pageIndex") int pageIndex,
                                       @RequestParam(required = true, value = "pageSize") int pageSize){
        try {
            Page page = PageHelper.startPage(pageIndex,pageSize);
            List<SysTest> list = sysTesService.findAll();
            Map result = assembledResult(page,list);
            return buildResult(result);
        }catch (Exception e){
            e.printStackTrace();
            return buildResult(Code.DEFAI.getValue(),"查询失败", HttpStatus.OK);
        }
    }

    @RequestMapping(value = "upload",method = RequestMethod.GET,headers = "Accept=application/json")
    public ResponseEntity<Map> upload(@RequestParam String link){
        String photoUrl = "";
        try {
            photoUrl = sysQiniuService.upload(link);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buildResult(photoUrl);
    }


}
